<?php

/**
 * Description of Menu
 *
 * @author PhongVan
 */
class Menu extends MenuBase {

    const MAIN_MENU 	= 1;
    const ADMIN_MENU 	= 2;
    const USER_MENU 	= 3;
    public $old_level;

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public function relations() {
        return array(
            'type' => array(self::BELONGS_TO, 'MenuType', 'type_id'),
            'parent' => array(self::BELONGS_TO, 'Menu', 'parent_id'),
            'childs' => array(self::HAS_MANY, 'Menu', 'parent_id', 'condition' => 'status=1'),
        );
    }

    public function getList() {
        $subitem = array();
        if ($this->childs) {
            foreach ($this->childs as $child) {
                $subitem[] = $child->getList();
            }
        }
        $arr = explode('/tag/', $this->url);
        if (empty($arr[1]))
            $this->url = $arr[0] != '#' ? array($arr[0]) : '';
        else
            $this->url = array($arr[0], 'tag' => $arr[1]);
        $return_array = array('label' => $this->name, 'url' => $this->url, 'visible' => ($this->visibleExpression != '') ? eval('return ' . $this->visibleExpression . ';') : true);

        if ($subitem != array()) {
            $return_array = array_merge($return_array, array('items' => $subitem));
        }
        return $return_array;
    }

    public function getSubMenu($id){
        $menu = Menu::model()->findAll(array('condition' => "parent_id={$id} and status=1", 'select' => 'id,name,alias,url'));
        if ($menu === null)
            return '';
        $html = "<div><ul>";

        foreach ($menu as $node):
            $html .= CHtml::openTag('li', array('class' => 'sub1_menu', 'id' => $node->id));

            if (strpos($node->url, 'http:') !== false)
                $link = CHtml::link($node->name, $node->url, array('class' => $node->alias));
            else
                $link = CHtml::link($node->name, Yii::app()->createUrl($node->url));

            $html .= $link;
            $html .= CHtml::tag('div', array('id' => 'sub2_container_' . $node->id, 'class' => 'sub2_menu', 'style'=>'margin-left: 80px'), '', true);
            $html .= CHtml::closeTag('li');
        endforeach;
        $html .= "</ul></div>";
        return $html;
    }

    public function defaultScope() {
        return array(
            'order' => 'level'
        );
    }

    public static function buildTree($typeID = null) {
        if ($typeID === null)
            return self::model()->findAll();
        else
            return self::model()->findAll('type_id=:type_id', array(':type_id' => $typeID));
    }

    public static function buildDataForList($tree, $eliminateId = null) {
        $list 			= array('<root>');
        $in_eliminateID = array();
        if ($eliminateId > 0) {
            $model = self::model()->findByPk($eliminateId);
            if ($model !== null) {
                $in_eliminateID = CHtml::listData(self::model()->findAll("level like '{$model->level}%'"), 'id', 'id');

            }
        }

        foreach ($tree as $node) {
            if ($node->id != $eliminateId && !in_array($node->id, $in_eliminateID)) {
                $level = count(explode("/", $node->level)) - 1;
                $list[$node->id] = str_repeat('-', $level * 5) . $node->name;
            }
        }
        return $list;
    }

    public function search() {
        $criteria = new CDbCriteria;
        if(Util::user()->id != 1)
            $criteria->compare('type_id', Menu::MAIN_MENU);

        $criteria->compare('id', $this->id, true);
        $criteria->compare('type_id', $this->type_id, true);
        $criteria->compare('parent_id', $this->parent_id);
        $criteria->compare('alias', $this->alias, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('url', $this->url, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination' => array(
                        'pageSize' => 100,
                    ),
                ));
    }

    public function beforeSave() {
        if ($this->isNewRecord)
            $this->create_time = date('Y-m-d h:i:s');
        else {
            $this->update_time = date('Y-m-d h:i:s');
            $this->old_level = $this->level;
            $level = number_format($this->weight / 100, 2, '', ',') . number_format($this->id / 100, 2, '', '');
            $this->level = ($this->parent_id > 0) ? $this->parent->level . '/' . $level : $level;
        }
        return true;
    }

    public function afterSave() {
        if ($this->isNewRecord) {
            $level = number_format($this->weight / 100, 2, '', '') . number_format($this->id / 100, 2, '', '');
            self::model()->updateByPk($this->id, array(
                'level' => ($this->parent_id > 0) ? $this->parent->level . '/' .$level : $level
            ));
        } else {
            if ($this->old_level != $this->level) {
                $temp = self::model()->findAll("level like '{$this->old_level}%'");
                if (is_array($temp)) {
                    foreach ($temp as $node) {
                        $node->level = str_replace($this->old_level, $this->level, $node->level);
                        $node->update('level');
                    }
                }
            }
        }
    }

}

?>
